package treatment;

import Money.Money;

import data_stuct.*;
import loader.*;
import util.*;

/**
 * Treatment class
 * 
 * @author Majid Mallis
 */
public class Treatment {

	private Loader loader;
	private ClassTable table1;
	private CorpoTable table2;

	/**
	 * Constructor: sets up the new Treatment class
	 * 
	 * @param l
	 */
	public Treatment(Loader l) {
		loader = l;
		table1 = new ClassTable();
		table2 = new CorpoTable();
	}

	/**
	 * collects utile data needed to calculate ratios by class
	 * 
	 */
	public void collectDataByClass() {

		Util.sortByClass(loader);

		char[] Class = new char['0'];
		char[] CA = new char['0'];

		for (Entry e : loader.getDb()) {
			String s1 = new String(Class);
			String s2 = new String(e.CENT_RES);
			// a new class to treat
			if (!s1.equals(s2)) {
				table1.getTable().add(new ClassEntry());
				table1.getTable().lastElement().CENT_RES = e.CENT_RES;
				Class = e.CENT_RES;
			}

			// This entry concerns a 'CA Clinique'
			if (Util.isCAClinique(e, loader.getTypesCA())) {
				String s3 = new String(CA);
				String s4 = new String(e.CENT_ACT);

				// a new CA to treat
				if (!s3.equals(s4)) {
					table1.getTable().lastElement().table.getTable().add(
							new CAEntry());
					table1.getTable().lastElement().table.getTable()
							.lastElement().CENT_ACT = e.CENT_ACT;
					CA = e.CENT_ACT;
				}
				table1.getTable().lastElement().table.getTable().lastElement().CT_BRUT1 += e.CT_BRUT;
				table1.getTable().lastElement().table.getTable().lastElement().HRS_TRAV1 += e.HRS_TRAV;
				table1.getTable().lastElement().table.getTable().lastElement().UnitA += e.UnitA;
				table1.getTable().lastElement().table.getTable().lastElement().UnitB += e.UnitB;
				table1.getTable().lastElement().HRS_TRAV += e.HRS_TRAV;

			} else { // 'CA de Soutien'
				table1.getTable().lastElement().CT_BRUT += e.CT_BRUT;
				table1.getTable().lastElement().HRS_TRAV += e.HRS_TRAV;
			}
		}
	}

	/**
	 * collects utile data needed to calculate ratios by corpo
	 * 
	 */
	public void collectDataByCorpo() {

		Util.sortByCorpo(loader);

		char[] Corpo = new char['0'];
		char[] CA = new char['0'];

		for (Entry e : loader.getDb()) {
			String s1 = new String(Corpo);
			String s2 = new String(e.CORPO);
			// a new corpo to treat
			if (!s1.equals(s2)) {
				table2.getTable().add(new CorpoEntry());
				table2.getTable().lastElement().CORPO = e.CORPO;
				Corpo = e.CORPO;
			}

			// This entry concerns a 'CA Clinique'
			if (Util.isCAClinique(e, loader.getTypesCA())) {
				String s3 = new String(CA);
				String s4 = new String(e.CENT_ACT);

				// a new CA to treat
				if (!s3.equals(s4)) {
					table2.getTable().lastElement().table.getTable().add(
							new CAEntry());
					table2.getTable().lastElement().table.getTable()
							.lastElement().CENT_ACT = e.CENT_ACT;
					CA = e.CENT_ACT;
				}
				table2.getTable().lastElement().table.getTable().lastElement().CT_BRUT2 += e.CT_BRUT;
				table2.getTable().lastElement().table.getTable().lastElement().HRS_TRAV2 += e.HRS_TRAV;
				table2.getTable().lastElement().table.getTable().lastElement().UnitA += e.UnitA;
				table2.getTable().lastElement().table.getTable().lastElement().UnitB += e.UnitB;
				table2.getTable().lastElement().HRS_TRAV += e.HRS_TRAV;

			} else { // 'CA de Soutien'
				table2.getTable().lastElement().CT_BRUT += e.CT_BRUT;
				table2.getTable().lastElement().HRS_TRAV += e.HRS_TRAV;
				CA = e.CENT_ACT;
			}
		}
		CA = new char['i'];
	}

	/**
	 * Calculates ratios by Class using SIFO procedure
	 * 
	 */
	public void calculateRatiosByClass() {
		for (ClassEntry ce : table1.getTable()) {
			for (CAEntry cae : ce.table.getTable()) {
				// Calcul of D1
				if (ce.HRS_TRAV != 0)
					cae.D1 = cae.CT_BRUT1 + cae.HRS_TRAV1 * ce.CT_BRUT
							/ ce.HRS_TRAV;
				// Calcul of EA1
				if (cae.UnitA != 0)
					cae.EA1 = cae.D1 / cae.UnitA;
				// Calcul of EB1
				if (cae.UnitB != 0)
					cae.EB1 = cae.D1 / cae.UnitB;
				// Calcul of F1
				if (cae.HRS_TRAV1 != 0)
					cae.F1 = cae.D1 / cae.HRS_TRAV1;
			}
		}
	}

	/**
	 * Calculates ratios by Corpo using SIFO procedure
	 * 
	 */
	public void calculateRatiosByCorpo() {
		for (CorpoEntry ce : table2.getTable()) {
			for (CAEntry cae : ce.table.getTable()) {
				// Calcul of D2
				if (ce.HRS_TRAV != 0)
					cae.D2 = cae.CT_BRUT2 + cae.HRS_TRAV2 * ce.CT_BRUT
							/ ce.HRS_TRAV;
				// Calcul of EA2
				if (cae.UnitA != 0)
					cae.EA2 = cae.D2 / cae.UnitA;
				// Calcul of EB2
				if (cae.UnitB != 0)
					cae.EB2 = cae.D2 / cae.UnitB;
				// Calcul of F2
				if (cae.HRS_TRAV2 != 0)
					cae.F2 = cae.D2 / cae.HRS_TRAV2;
			}
		}
	}

	public Object[][] RatiosByClasstoPrintableTable() {
		// Calculate dimension for array allocation
		int dim = 0;
		for (ClassEntry ce : table1.getTable()) {
			dim = dim + ce.table.getTable().size();
		}

		Object[][] dataTable = new Object[dim][10];
		int i = 0;

		for (ClassEntry ce : table1.getTable()) {
			for (CAEntry cae : ce.table.getTable()) {
				int j = 0;
				dataTable[i][j++] = new String(ce.CENT_RES);
				dataTable[i][j++] = new String(cae.CENT_ACT);
				dataTable[i][j++] = new Money(((Math
						.floor(cae.CT_BRUT1 * 100 + 0.5)) / 100)).toString();
				dataTable[i][j++] = new Money(
						(Math.floor(cae.D1 * 100 + 0.5)) / 100).toString();
				dataTable[i][j++] = cae.UnitA;
				dataTable[i][j++] = new Money(
						((Math.floor(cae.EA1 * 100 + 0.5)) / 100)).toString();
				dataTable[i][j++] = cae.UnitB;
				dataTable[i][j++] = new Money(
						((Math.floor(cae.EB1 * 100 + 0.5)) / 100)).toString();
				dataTable[i][j++] = cae.HRS_TRAV1;
				dataTable[i][j++] = new Money(
						((Math.floor(cae.F1 * 100 + 0.5)) / 100)).toString();
				i++;
			}
		}

		return dataTable;
	}

	public Object[][] RatiosByCorpotoPrintableTable() {
		// Calculate dimension for array allocation
		int dim = 0;
		for (CorpoEntry ce : table2.getTable()) {
			dim = dim + ce.table.getTable().size();
		}

		Object[][] dataTable = new Object[dim][10];
		int i = 0;

		for (CorpoEntry ce : table2.getTable()) {
			for (CAEntry cae : ce.table.getTable()) {
				int j = 0;
				dataTable[i][j++] = new String(ce.CORPO);
				dataTable[i][j++] = new String(cae.CENT_ACT);
				dataTable[i][j++] = new Money(((Math
						.floor(cae.CT_BRUT2 * 100 + 0.5)) / 100)).toString();
				dataTable[i][j++] = new Money(
						(Math.floor(cae.D2 * 100 + 0.5)) / 100).toString();
				dataTable[i][j++] = cae.UnitA;
				dataTable[i][j++] = new Money(
						((Math.floor(cae.EA2 * 100 + 0.5)) / 100)).toString();
				dataTable[i][j++] = cae.UnitB;
				dataTable[i][j++] = new Money(
						((Math.floor(cae.EB2 * 100 + 0.5)) / 100)).toString();
				dataTable[i][j++] = cae.HRS_TRAV2;
				dataTable[i][j++] = new Money(
						((Math.floor(cae.F2 * 100 + 0.5)) / 100)).toString();
				i++;
			}
		}

		return dataTable;
	}
}
